home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / util / ResourceUtil.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-20  |  3.7 KB  |  104 lines

  1. package asp.util;
  2.  
  3. import java.awt.Component;
  4. import java.util.Hashtable;
  5. import java.util.ResourceBundle;
  6.  
  7. public class ResourceUtil {
  8.    public static final String RESBASE_ASPWIZ = "asp.wizard.res";
  9.    public static final String RESBASE_ASPCOMP = "asp.nfx.res";
  10.    private static Hashtable _resourceUtilDictionary = new Hashtable();
  11.    ResourceBundle _rb;
  12.  
  13.    public static ResourceUtil getResourceUtil(String resourceBase, Class classOfPanel) throws EResourceUtil {
  14.       ResourceUtil ru = null;
  15.       if (classOfPanel != null) {
  16.          ru = (ResourceUtil)_resourceUtilDictionary.get(classOfPanel);
  17.          if (ru == null) {
  18.             ru = new ResourceUtil(resourceBase, classOfPanel);
  19.             _resourceUtilDictionary.put(classOfPanel, ru);
  20.          }
  21.       }
  22.  
  23.       return ru;
  24.    }
  25.  
  26.    public ResourceUtil(String resourceBase, Class componentClass) throws EResourceUtil {
  27.       if (componentClass == null) {
  28.          throw new EResourceUtil("ResourceTool needs to know the resource user class");
  29.       } else {
  30.          String bundleName = (resourceBase != null ? resourceBase + "." : "") + "Resource" + getRightToLastDot(componentClass.getName());
  31.          this._rb = ResourceBundle.getBundle(bundleName);
  32.       }
  33.    }
  34.  
  35.    public static String getResourceString(String resourceBase, Class classOfPanel, String key) {
  36.       ResourceUtil ru = null;
  37.       String rs = "";
  38.  
  39.       try {
  40.          ru = getResourceUtil(resourceBase, classOfPanel);
  41.          if (ru != null) {
  42.             rs = ru.getString(key);
  43.          }
  44.       } catch (EResourceUtil var5) {
  45.       }
  46.  
  47.       return rs;
  48.    }
  49.  
  50.    public String getString(String key) {
  51.       return this._rb != null ? this._rb.getString(key) : null;
  52.    }
  53.  
  54.    public String getString(String key, String defstr) {
  55.       String result = this.getString(key);
  56.       if (result == null) {
  57.          result = defstr;
  58.       }
  59.  
  60.       return result;
  61.    }
  62.  
  63.    public static String getString(String key, Component child) {
  64.       while(child != null && !(child instanceof ResourceUtilOwner)) {
  65.          child = child.getParent();
  66.       }
  67.  
  68.       return child != null ? ((ResourceUtilOwner)child).getResourceUtil().getString(key) : null;
  69.    }
  70.  
  71.    public char getMnemonic(String key) {
  72.       char result = 0;
  73.       if (key != null) {
  74.          String resStr = this.getString(key + ".mnemonic");
  75.          if (resStr != null && resStr.length() > 0) {
  76.             result = resStr.charAt(0);
  77.          }
  78.       }
  79.  
  80.       return result;
  81.    }
  82.  
  83.    private static String getRightToLastDot(String str) {
  84.       String result = null;
  85.       if (str != null) {
  86.          int dotloc = str.lastIndexOf(46);
  87.          result = dotloc != -1 ? str.substring(dotloc + 1) : str;
  88.       }
  89.  
  90.       return result;
  91.    }
  92.  
  93.    public String getComponentString(Component comp) {
  94.       String result = "";
  95.       if (comp != null) {
  96.          String classname = getRightToLastDot(comp.getClass().getName());
  97.          String reskey = classname + '.' + comp.getName();
  98.          result = this.getString(reskey);
  99.       }
  100.  
  101.       return result;
  102.    }
  103. }
  104.